# -*- coding: utf-8 -*-
import unittest
from yacf.Tools import SourceStorage

from yacf.Backends.C.Files.FileTypes import C_FileType, H_FileType
from yacf.Backends.Common.Files.FileTypes import Template_FileType

file1 = """
    int main () 
    {
        return 0;
    }
    """
fileName1 = 'main.c'

fileN1 = 'main'

fileName2 = 'main.h'

class TestSourceStorageFunctions(unittest.TestCase):
    def setUp(self):
        self.ss = SourceStorage.Storage ('.','/output')
        self.ss2 = SourceStorage.Storage ('.', '/output2')
        self.cType = C_FileType ()
        self.hType = H_FileType ()
          
    def test_files(self):
        # First of all, the number of elements must be 0
        self.assertEqual(0, self.ss.getNumberOfElements())
        # Add a file
        self.ss.addFile(fileN1, self.cType)
        # Append some text
        self.ss.append(fileN1, self.cType, file1)
        # Some tests
        self.assertEqual(True, self.ss.existsElement(fileName1))
        self.assertEqual(self.ss.getElement(fileName1)[0], self.cType)
        self.assertEqual(self.ss.getElement(fileName1)[1], file1)
        # Now, the number of elements must be 1
        self.assertEqual(1, self.ss.getNumberOfElements())
        # Add a second file
        self.ss.addFile(fileN1, self.hType)
        self.ss.append(fileN1, self.hType, "Text to be overwritten")
        # Overwrite the content
        self.ss.overwrite(fileN1, self.hType, "New text")
        self.assertEqual("New text", self.ss.getElement(fileName2)[1])
        # Get all the files
        self.assertEqual(['main.c', 'main.h'], self.ss.getAllNames())
    
    def test_similarity(self):
        self.ss.addFile(fileN1, self.cType)
        self.ss.append(fileN1, self.cType, file1)
        self.ss2.addFile(fileN1, self.cType)
        self.ss2.append(fileN1, self.cType, file1)
        # Both objects are the same
        self.assertEqual(True, self.ss.isTheSame(self.ss2)[0])
        self.ss2.append(fileN1, self.cType, '\ntrash')
        # Now, ss2 is different
        self.assertEqual(False, self.ss.isTheSame(self.ss2)[0])
        differences = self.ss.isTheSame(self.ss2)[1]
        # Only one file is different
        self.assertEqual(1,len(differences))
        # check the name of the file with differences
        self.assertEqual(fileName1, differences[0][0])
        # check the differences
        self.assertEqual(['  ', '      int main () ', '      {', '          return 0;', '      }', '      ', '+ trash'], differences[0][1])
        # Now, the function copy
        self.ss.copy(self.ss2)
        self.assertEqual(True, self.ss.isTheSame(self.ss2)[0])
        
    def test_frost_defrost(self):
        self.ss.addFile(fileN1, self.cType)
        self.ss.append(fileN1, self.cType, "Some text")
        self.ss.freeze(".", "congelamiento.txt")
        # ss2 get the frozen copy of ss 
        self.ss2.defrost(".", "congelamiento.txt")
        # ss and ss2 must be the same
        self.assertEqual(True, self.ss.isTheSame(self.ss2)[0])
        # Test the written files
        self.ss.closeStorage()
        filename = './output/' + fileName1
        tmp_file = open(filename, 'r')
        content = tmp_file.read()
        self.assertEqual("Some text", content)
        tmp_file.close()
    
    def test_FileTypesFunctions(self):
        # C code files
        self.assertEqual (self.cType.name, "C")
        self.assertEqual (self.cType.extension, ".c")
        # Template files
        template = Template_FileType()
        template_text = "Hello ${name}. This is a test"
        new_text = template.render(template_text, name="Peter")
        self.assertEqual ("Hello Peter. This is a test", new_text)
        
        
        
        
if __name__ == '__main__':
    unittest.main()